<template>
  <div class="marBox">
    <el-row>
      <el-col class="titel-col" :span="4">主题颜色选取：</el-col>
      <el-col :span="20">
        <el-color-picker v-model="blogBgColor" @change="clolorClick" @active-change="colorChange"></el-color-picker>
      </el-col>
    </el-row>
    <el-row>
      <el-col class="titel-col" :span="4">主题背景图选取：</el-col>
      <el-col :span="20">
        <div class="imgsBox">
          <div class="imgBox" 
              v-for="item in bgImgList" 
              @click="selImg(item)" 
              :key="item">
            <div class="imgShowBox" :style="{backgroundImage:getImgPath(item)}"></div>
            <div class="imgSelBox" :class="{'imgSelBox1':item===bgImgPath}">
              <i class="el-icon-check"></i>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col class="titel-col" :span="4">主题字体颜色切换：</el-col>
      <el-col :span="20">
        <el-switch
          v-model="fontColorTab"
          @change="fontColorSwitch"
          active-color="#242424"
          inactive-color="#B4B4B4"
        ></el-switch>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      blogBgColor:'',
      bgImgPath:'',
      fontColorTab:'',
      bgImgList: [
        "blogImg1",
        "blogImg2",
        "blogImg3",
        "blogImg4",
        "blogImg5",
        "blogImg6",
        "blogImg7",
        "blogImg8",
        "blogImg9",
        "blogImg10",
        "blogImg11",
        "blogImg12"
      ]
    };
  },
  props:['fontColor','bgImgUrl','blogColor'],
  mounted(){
      let blogSet=JSON.parse(localStorage.getItem('blogSet'))
      if(blogSet){
        this.blogBgColor= blogSet.blogColor;
        this.bgImgPath= blogSet.blogBgImg;
        this.fontColorTab= blogSet.blogFontColor;
      }
      
  },
  methods: {
    getImgPath(imgName) {
      return (
        "url(" + require("./../../../assets/bgImg/" + imgName + ".png") + ")"
      );
    },
    colorChange(val) {
      this.$emit("setBgColor", val);
      this.setForStorage()
    },
    clolorClick() {
      this.$store.commit("setBlogColor", this.blogBgColor);
      this.setForStorage()

    },
    fontColorSwitch() {
      this.$emit("setFontColor", this.fontColorTab);
      this.$store.commit("setBlogFontColor", this.fontColorTab);
      this.setForStorage()

    },
    selImg(item){
      this.bgImgPath=item;
      this.$emit("setBgImg", this.bgImgPath);
      this.$store.commit("setBlogBgImg", this.bgImgPath);
      this.setForStorage()
    },
    setForStorage(){
      let blogSet={
        blogColor:this.blogBgColor,
        blogBgImg:this.bgImgPath,
        blogFontColor:this.fontColorTab,
      }
      localStorage.setItem('blogSet',JSON.stringify(blogSet))
    }
  }
};
</script>

<style lang="scss" scoped>
.marBox {
  width: 80%;
  margin: 5px auto;
  padding: 30px 5%;
  background-color: #f1f1f1;
  .titel-col {
    font-weight: bold;
    padding-right: 10px;
    text-align: right;
  }
  .titel-row {
    margin: 20px 0px;
  }
  .imgsBox {
    display: flex;
    flex-wrap: wrap;
    .imgBox {
      width: 100px;
      height: 100px;
      margin: 10px;
      overflow: hidden;
      cursor: pointer;
      border-radius: 2px;
      box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);

      .imgShowBox {
        width: 100%;
        height: 100%;
      }
      .imgSelBox,
      .imgSelBox1 {
        width: 100%;
        height: 100%;
        transition: all 0.2s 0s linear;
        background-color: rgba(0, 0, 0, 0.1);
        //background-color: red;
        display: flex;
        justify-content: center;
        font-size: 80px;
        color: green;
        i {
          margin: 0 auto;
          line-height: 130%;
          font-weight: bolder;
        }
      }
      .imgSelBox1 {
        transform: translate(0px, -100px);
      }
    }
    .imgBox:hover {
      .imgSelBox {
        transform: translate(0px, -100px);
      }
    }
  }
}
</style>